package com.cd2cd.code_template.curd.annotation;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
public class CustomCurdRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

    /**
     * cache custom service
     */
    public static final Map<String, String> CUSTOM_SERVICE = new HashMap<>();

    private  ResourceLoader loader;
    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment ;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {

        ClassPathScanningCandidateComponentProvider scaner = new ClassPathScanningCandidateComponentProvider(false, this.environment);
        scaner.setResourceLoader(loader);
        //添加注解过滤器，只扫描 CustomCurdService 注解的类
        AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(CustomCurdService.class);
        scaner.addIncludeFilter(annotationTypeFilter);

        //默认扫描主类下的包
        String classPathPackage = ClassUtils.getPackageName(annotationMetadata.getClassName());
        Set<BeanDefinition> candidateComponets = scaner.findCandidateComponents(classPathPackage);

        for (BeanDefinition component : candidateComponets) {

            AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition)component;
            AnnotationMetadata metadata = beanDefinition.getMetadata();

            Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(CustomCurdService.class.getCanonicalName());
            Class c = (Class) annotationAttributes.get("value");

            String doMain = c.getSimpleName();
            String service = component.getBeanClassName();

            String typeName = c.getTypeName();

            String[] ss = service.split("\\.");
            String cusName = StringUtils.uncapitalize(ss[ss.length - 1]);

            String value = cusName+"|"+typeName;
            CUSTOM_SERVICE.put(doMain, value);
        }

    }
}
